using Behaving;
using Behaving.Actions;
using Behaving.Composites;
using FluentAssertions;
using NUnit.Framework;

namespace Behaving.Tests.CompositeTests
{
    [TestFixture]
    public class SequenceTests
    {
        [Test]
        public void When_a_sequence_is_run()
        {
            var firstRun = false;
            int secondRun = 0;
            var thirdRun = false;
            BehaviourTree behaviourTree = new BehaviourTree();
            behaviourTree.Root = new Sequence(
                new ActionBehaviour(
                    () => firstRun = true),
                new ActionBehaviour(() =>
                {
                    secondRun++;
                    if (secondRun < 3)
                    {
                        return RunningState.Running;
                    }
                    else
                    {
                        return RunningState.Success;
                    }
                }),
                new ActionBehaviour(() => thirdRun = true));

            behaviourTree.Tick();
            firstRun.Should().BeTrue();
            secondRun.Should().Be(1);
            thirdRun.Should().BeFalse();

            behaviourTree.Tick();
            firstRun.Should().BeTrue();
            secondRun.Should().Be(2);
            thirdRun.Should().BeFalse();

            behaviourTree.Tick();
            firstRun.Should().BeTrue();
            secondRun.Should().Be(3);
            thirdRun.Should().BeTrue();
        }

        [Test]
        public void When_a_sequence_is_run_with_repeating_first()
        {
            ExecutionCounter erunner = new ExecutionCounter();
            BehaviourTree behaviourTree = new BehaviourTree();
            behaviourTree.Root = new Sequence(
                erunner = new ExecutionCounter(RunningState.Running),
                new SuccessAction());

            behaviourTree.Tick();
            Assertions.AssertWasStartedAndExecuted(erunner);

            behaviourTree.Tick();
            Assertions.AssertWasExecuted(erunner);

            behaviourTree.Tick();
            Assertions.AssertWasExecuted(erunner);
        }      
    }
}